/*
 *  Copyright 2009 Advanced Industrial Science and Technology (AIST).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file
 *  \brief
 */
#pragma once

#include <thrust/graph/detail/adjacency_iterator_base.hpp>

namespace thrust {

  namespace detail {  
  struct adjacency_iter_friend;
  } //end namespace detail
  
  template <class Graph, class Vertex, class OutEdgeIter>
  struct adjacency_iter
    : public detail::adjacency_iter_base<Graph,Vertex,OutEdgeIter>::type
  {
  private:
    typedef adjacency_iter<Graph,Vertex,OutEdgeIter> self_t;
    typedef typename detail::adjacency_iter_base<Graph,Vertex,OutEdgeIter>::type super_t;

    typedef typename OutEdgeIter::called_space called_space;

    friend class experimental::iterator_core_access;
    friend struct detail::adjacency_iter_friend;
    
  private:
    const Graph* m_g;
    //OutEdgeIter m_iter;
    
  public:
    __host__ __device__
    inline adjacency_iter() 
      : m_g(0) 
    {}
    
    __host__ __device__
    inline adjacency_iter( OutEdgeIter const& iter, const Graph& g)
      : super_t(iter), m_g(&g)
    {}
    
  private:
    __host__ __device__
    inline Vertex
    dereference() const 
    { 
      return dereference( called_space());
    }

    __host__ __device__
    inline Vertex
    dereference( thrust::called_from_host_tag) const 
    {
      // this->base() is out_edge_iterator
      const OutEdgeIter& iter =this->base();
      return target((*iter), *m_g);
    }
    
    __device__
    inline Vertex
    dereference( thrust::called_from_device_tag) const 
    {
      // XXX : this->base() is kernel_out_edge_iterator
      const OutEdgeIter& iter =this->base();
      return target((*iter), *m_g);
    }
  };

} // end namespace thrust
#include <thrust/graph/detail/adjacency_iterator.inl>
